package com.clouddot.baipai.http;

import android.content.Context;

import com.clouddot.baipai.infrastructure.environment.Env;

import org.wxy.sdk.BuildConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by wxy on 2018/5/16.
 */

public class PushLeaveNoRetrofitHelper {
    private Context mCntext;
    private static PushLeaveNoRetrofitHelper instance = null;
    private Retrofit mRetrofit = null;
    private IPushLeaveNoApi api = null;


    public static PushLeaveNoRetrofitHelper getInstance(Context context) {
        if (instance == null) {
            instance = new PushLeaveNoRetrofitHelper(context);
        }
        return instance;
    }

    public void reSet() {
        resetApp();
        api = mRetrofit.create(IPushLeaveNoApi.class);
    }

    public IPushLeaveNoApi getServer() {
        if (api == null) {
            api = mRetrofit.create(IPushLeaveNoApi.class);
        }
        return api;
    }

    private PushLeaveNoRetrofitHelper(Context mContext) {
        mCntext = mContext;
        init();
    }

    private void init() {
        resetApp();
    }

    private void resetApp() {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.readTimeout(10, TimeUnit.SECONDS);
        builder.connectTimeout(10, TimeUnit.SECONDS);
        builder.cookieJar(new CookieStore());
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(interceptor);
        }
        mRetrofit = new Retrofit.Builder().baseUrl(Env.getAppContext().pushLeaveNoUrl).client(builder.build())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(new NullOnEmptyConverterFactory())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

    }


}
